let express = require("express");
let path = require("path");
let fs = require("fs");
let router = express.Router();
let app = express();
let bodyParser = require("body-parser");

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(router);

router.all("*", function (req, res, next) {
  res.header("Access-Control-Allow-Origin", "*");
  res.header("Access-Control-Allow-Headers", "Content-Type");
  res.header("Access-Control-Allow-Methods", "*");
  res.header("Content-Type", "application/json;charset=utf-8");
  next();
});
router.post("/", (req, res, next) => {
  // let list = [];
  // list.push(req.body);

  // console.log(req.body);

  fs.appendFile(
    path.join(__dirname, "data.json"),
    // JSON.stringify(list),
    JSON.stringify(req.body),
    function (err) {
      if (err) {
        throw err;
      }
      console.log(list);
      console.log("ok");
    }
  );
});

app.listen("3000", function () {
  console.log("running...");
});
